package OnTheBeach::Controller::Forum;

use strict;
use warnings;
use base 'Catalyst::Controller';
use DateTime;


=head1 NAME

OnTheBeach::Controller::Forum - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 index 

Get the thread "summary" and the comment count.
Needs to be extracted and fleshed out into a separate sub.

=cut

sub index : Private {

	my ( $self, $c ) = @_;
	my @threads;

	## set up the query to get all threads that aren't children
	my $rs = $c->model('OnTheBeachDB::Message')->search( { parent_id => '0' },
		{ order_by => 'created DESC', rows => '10' } );

	## loop through the parent threads,
	## count the number of children,
	## and push a reference to %thread_info into @threads

	while ( my $thread = $rs->next ) {

		my %thread_info;

		$thread_info{'name'}          = $thread->name;
		$thread_info{'created'}       = $thread->created;
		$thread_info{'title'}         = $thread->title;
		$thread_info{'message_id'}    = $thread->message_id;
		$thread_info{'message'}       = $thread->message;
		$thread_info{'comment_count'} = $thread->children->count;
		$thread_info{'parent_thread'} = $thread->parent;
		push( @threads, \%thread_info )

	}

	### Will need this for pagination
	#my $pager = $blog->pager;
	#$c->stash->{next_page}      = $pager->next_page;
	#$c->stash->{prev_page}      = $pager->previous_page;
	#$c->stash->{first_page}     = $pager->first_page;
	#$c->stash->{last_page}      = $pager->last_page;
	#$c->stash->{current_page}   = $pager->current_page;
	#$c->stash->{blogs} = [$blog->all];
	#$c->stash->{template} = 'blog/list.tt2';

	$c->stash->{page} =
	  $c->model('OnTheBeachDB::Page')->find( { name => "forum_main" } );

	$c->stash->{categories} = [ $c->model('OnTheBeachDB::Forum')->all ];
	$c->stash->{threads}    = \@threads;
}

=head2 create  

Create a new thread.

=cut

sub create : Local {
	my ( $self, $c, $forum ) = @_;

	$forum =~ /\d+/;
	$c->stash->{forum_id} = $forum;

}

sub do_create : Local {
	my ( $self, $c ) = @_;
	## here we need to clean the message
	my $name    = $c->req->param('name');
	my $title   = $c->req->param('title');
	my $message = $c->req->param('message');
	my $now     = DateTime->now;
	my $forum   = $c->req->param('forum');

        unless ( $c->validate_captcha( $c->req->param('captcha_text') ) ) {

            $c->res->redirect( 
                $c->uri_for('create', { error_msg => 'Invalid CAPTCHA text' } )
            );
            $c->detach();

        }

	my $thread = $c->model('OnTheBeachDB::Message')->create(
		{
			name     => $name,
			title    => $title,
			message  => $message,
			created  => $now,
			forum_id => $forum,

		}
	);

	$c->stash->{thread_id} = $thread->message_id;

}

sub reply : Local {
	my ( $self, $c, $replyid ) = @_;

	my $thread = $c->model('OnTheBeachDB::Message')->find($replyid);
	$c->stash->{forum}  = $thread->forum_id;
	$c->stash->{thread} = $thread;

}

sub do_reply : Local {

	my ( $self, $c ) = @_;

	my $thread = $c->model('OnTheBeachDB::Message')->create(
		{
			name      => $c->req->param('name'),
			title     => $c->req->param('title'),
			message   => $c->req->param('comments'),
			parent_id => $c->req->param('parent_id'),
			created   => DateTime->now,
			forum_id  => $c->req->param('forum'),

		}
	);

	$c->stash->{thread_id} = $thread->message_id;
	$c->stash->{template}  = 'forum/do_create.tt2';

}

sub view : Local {
	my ( $self, $c, $id ) = @_;

	## check to make sure the id is a number
	if ( $id !~ /\d+/ ) {

		$c->res->redirect(
			$c->uri_for(
				'/forum', { error_msg => "Post ID must be a number." }
			)
		);
		return;

	}

	## make sure our post exists
	eval {

		my $thread =
		  $c->model('OnTheBeachDB::Message')
		  ->get_thread_messages( $id, 'replies' );

		$c->stash->{threads} = $thread;

	};

	## something went wrong, throw an error
	if ($@) {

		$c->res->redirect(
			$c->uri_for( '/forum', { error_msg => "Post doesn't exist." } ) );
		return;

	}

}

sub category : Local {
	my ( $self, $c, $id ) = @_;

	# check to see if the category exists
	my $category_exists =
	  $c->model('OnTheBeachDB::Forum')->search( { forum_id => $id } )->count;

	if ( !$category_exists ) {
		$c->res->redirect(
			$c->uri_for(
				'/forum', { error_msg => "Category/Forum doesn't exist!" }
			)
		);
		return;
	}

	$c->stash->{current_forum_id} = $id;

	## category info
	my $category = $c->model('OnTheBeachDB::Forum')->find($id);
	$c->stash->{forum_description} = $category->description;
	$c->stash->{forum_name}        = $category->name;

	my @threads;

	## set up the query to get all threads that aren't children
	my $rs = $c->model('OnTheBeachDB::Message')->search(
		{ parent_id => '0',            forum_id => $id },
		{ order_by  => 'created DESC', rows     => '10' }
	);

	## loop through the parent threads,
	## count the number of children,
	## and push a reference to %thread_info into @threads
	#### NEEDS TO BE IN A SUB ####

	while ( my $thread = $rs->next ) {

		my %thread_info;

		$thread_info{'name'}          = $thread->name;
		$thread_info{'created'}       = $thread->created;
		$thread_info{'title'}         = $thread->title;
		$thread_info{'message_id'}    = $thread->message_id;
		$thread_info{'message'}       = $thread->message;
		$thread_info{'comment_count'} = $thread->children->count;
		$thread_info{'parent_thread'} = $thread->parent;
		push( @threads, \%thread_info )

	}
	$c->stash->{forum_threads} = \@threads;

}

=head1 AUTHOR

devin@onthebeachatnight.com

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
